package com.ruoyi.delivery.controller;

import com.ruoyi.delivery.domain.DelOperatorTransportDetail;
import com.ruoyi.delivery.domain.DelOperatorTransportOrder;
import com.ruoyi.delivery.service.IDelOperatorTransportOrderService;
import org.apache.ibatis.annotations.Param;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * 运输订单 控制层
 *
 * @author hu
 */

@RestController
@RequestMapping("/delivery/operatorTransportOrder")
public class DelOperatorTransportOrderController {

    @Autowired
    private IDelOperatorTransportOrderService OperatorTransportOrderService;

    /**
     * 获取运输订单列表，支持分页和筛选
     *
     * @param trackingNumber 运单号
     * @param origin         发货地
     * @param destination    目的地
     * @param departStartDate 调拨日期范围的起始日期
     * @param departEndDate 调拨日期范围的结束日期
     * @param arriveStartDate 预计到达日期范围的起始日期
     * @param arriveEndDate 预计到达日期范围的结束日期
     * @param state          运单状态
     * @param customerType   客户类型
     * @param page           页码
     * @param pageSize       每页记录数
     * @return 分页运输订单列表
     */
    @GetMapping("/list")
    public Map<String, Object> getOperatorTransportOrderList(
            @RequestParam(required = false) String logisticId,
            @RequestParam(required = false) String trackingNumber,
            @RequestParam(required = false) String origin,
            @RequestParam(required = false) String destination,
            @RequestParam(required = false) String departStartDate,
            @RequestParam(required = false) String departEndDate,
            @RequestParam(required = false) String arriveStartDate,
            @RequestParam(required = false) String arriveEndDate,
            @RequestParam(required = false) String state,
            @RequestParam(required = false) String customerType,
            @RequestParam(defaultValue = "1") int page,
            @RequestParam(defaultValue = "10") int pageSize
    ){
        // 获取数据
        List<DelOperatorTransportOrder> orders = OperatorTransportOrderService.filterOperatorTransportOrders(
                logisticId, trackingNumber, origin, destination, departStartDate, departEndDate, arriveStartDate,arriveEndDate, state, customerType, page, pageSize
        );

        //获取数据总数
        int total = OperatorTransportOrderService.getOperatorTransportOrderCount(
                logisticId, trackingNumber, origin, destination, departStartDate, departEndDate, arriveStartDate,arriveEndDate, state, customerType
        );

        // 组装分页信息
        Map<String, Object> response = new HashMap<>();
        response.put("data", orders);
        response.put("total", total);
        response.put("page", page);
        response.put("pageSize", pageSize);
        return response;
    }

    /**
     * 添加运输订单
     *
     * @param order 运输订单对象
     * @return 操作结果
     */
    @PostMapping("/add")
    public Map<String, Object> addOperatorTransportOrder(@RequestBody DelOperatorTransportOrder order) {
        boolean success = OperatorTransportOrderService.insertOperatorTransportOrder(order);
        Map<String, Object> response = new HashMap<>();
        response.put("success", success);
        return response;
    }

    /**
     * 删除运输订单
     *
     * @param trackingNumber 运单号
     * @return 操作结果
     */
    @DeleteMapping("/delete")
    public Map<String, Object> deleteOperatorTransportOrder(@RequestParam String trackingNumber) {
        boolean success = OperatorTransportOrderService.deleteOperatorTransportOrder(trackingNumber);
        Map<String, Object> response = new HashMap<>();
        response.put("success", success);
        return response;
    }

    /**
     * 修改运输订单
     *
     * @param order 运输订单对象
     * @param approach 修改方式
     * @return 操作结果
     */
    @PutMapping("/update")
    public Map<String, Object> updateOperatorTransportOrder(@RequestBody DelOperatorTransportOrder order, @RequestParam String approach) {
        if(approach.equals("edit")) {
            order.setState("待确认");
        }
        boolean success = OperatorTransportOrderService.updateOperatorTransportOrder(order);
        Map<String, Object> response = new HashMap<>();
        response.put("success", success);
        return response;
    }

    /**
     * 获取运输订单明细
     *
     * @param trackingNumber 运单号
     * @return 运输订单明细列表
     */
    @GetMapping("/details")
    public Map<String, Object> getOperatorTransportOrderDetails(@RequestParam String trackingNumber) {
        List<DelOperatorTransportDetail> details = OperatorTransportOrderService.getDetailsByTrackingNumber(trackingNumber);
        Map<String, Object> response = new HashMap<>();
        response.put("data", details);
        return response;
    }

    /**
     * 确认运输订单
     *
     * @param trackingNumber 运单号
     * @return 操作结果
     */
    @PutMapping("/confirm")
    public Map<String, Object> confirmOperatorTransportOrder(@RequestParam String trackingNumber) {
        Integer success = OperatorTransportOrderService.confirmOperatorTransportOrder(trackingNumber,"1009898");
        Map<String, Object> response = new HashMap<>();
        response.put("success", success);
        return response;
    }
}
